<?php


namespace app\admin\controller;

use think\Db;


class JoinMessage extends Base
{
    // 留言展示
    public function index()
    {
        $where = [];
        $where['status'] = ['>', 0];
        $message_name = input('message_name');

        if (!empty($message_name)) {
            $where['name'] = ['like', '%' . $message_name . '%'];
        }

        $list = Db::name('message')
            ->where($where)
            ->order('update_time desc')
            ->field('id, name, phone, address, message, create_time, update_time')
            ->paginate(config('paginate.list_rows'), false, ['query' => get_query()]);
        $page = $list->render();

        $this->assign('data', $list);
        $this->assign('message_name', $message_name);
        $this->assign('page', $page);

        if (request()->isAjax()) {
            return $this->fetch('ajax_index');
        } else {
            return $this->fetch();
        }
    }

    // 修改留言
    public function edit()
    {
        $id = input('id');
        $info = Db::name('message')
            ->where('id', $id)
            ->find();
        $this->assign('info', $info);

        return $this->fetch();
    }

    // 删除留言图片
    public function delPic()
    {
        $id = input('id');
        $info = Db::name('image')->where('id',$id)->delete();

        if($info){
            $result = array(
                'status' => 1,
                'msg' => '删除成功!'
            );
        }else{
            $result = array(
                'status' => 0,
                'msg' => '删除失败!'
            );
        }

        return($result);
    }

    //新增留言图片
    public function addPic() {
        $message_id = input('message_id');
        $info = input('info/a');
        $data = [];
        foreach($info as $k=>$v){
            $data[$k]['url'] = $v;
            $data[$k]['id'] = Db::name('image')->insertGetId(['url'=>$v]);
            Db::name('message')->where('id', $message_id)->update(['img_id'=>$data[$k]['id']]);
        }

        if($data){
            $result = array(
                'status' => 1,
                'msg' => '上传成功!',
                'arr' => $data
            );
        }else{
            $result = array(
                'status' => 0,
                'msg' => '上传失败!'
            );
        }

        return($result);
    }

    // 处理修改留言
    public function doEdit()
    {
        $id = input('id');
        $data = [];
        $data['name'] = input('name');
        $data['phone'] = input('phone');
        $data['address'] = input('address');
        $data['message'] = input('message');
        $data['update_time'] = time();

        $info = Db::name('message')->where('id', $id)->update($data);

        if ($info !== false) {
            $this->success('修改成功!', url('admin/JoinMessage/index'));
        } else {
            $this->error('修改失败,请重试');
        }
    }

    // 删除留言
    public function del()
    {
        $id = input('id');
        $info = Db::name('message')->where('id', $id)->update(['status' => 0]);

        if ($info) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}